home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / kgdb.sun3 / valops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-11  |  32.6 KB  |  1,250 lines

  1. /* Perform non-arithmetic operations on values, for GDB.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "stdio.h"
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "symtab.h"
  24. #include "value.h"
  25. #include "frame.h"
  26. #include "inferior.h"
  27.  
  28. /* Cast value ARG2 to type TYPE and return as a value.
  29.    More general than a C cast: accepts any two types of the same length,
  30.    and if ARG2 is an lvalue it can be cast into anything at all.  */
  31.  
  32. value
  33. value_cast (type, arg2)
  34.      struct type *type;
  35.      register value arg2;
  36. {
  37.   register enum type_code code1;
  38.   register enum type_code code2;
  39.   register int scalar;
  40.  
  41.   /* Coerce arrays but not enums.  Enums will work as-is
  42.      and coercing them would cause an infinite recursion.  */
  43.   if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
  44.     COERCE_ARRAY (arg2);
  45.  
  46.   code1 = TYPE_CODE (type);
  47.   code2 = TYPE_CODE (VALUE_TYPE (arg2));
  48.   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
  49.         || code2 == TYPE_CODE_ENUM);
  50.  
  51.   if (code1 == TYPE_CODE_FLT && scalar)
  52.     return value_from_double (type, value_as_double (arg2));
  53.   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
  54.        && (scalar || code2 == TYPE_CODE_PTR))
  55.     return value_from_long (type, value_as_long (arg2));
  56.   else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
  57.     {
  58.       VALUE_TYPE (arg2) = type;
  59.       return arg2;
  60.     }
  61.   else if (VALUE_LVAL (arg2) == lval_memory)
  62.     {
  63.       return value_at (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
  64.     }
  65.   else
  66.     error ("Invalid cast.");
  67. }
  68.  
  69. /* Create a value of type TYPE that is zero, and return it.  */
  70.  
  71. value
  72. value_zero (type, lv)
  73.      struct type *type;
  74.      enum lval_type lv;
  75. {
  76.   register value val = allocate_value (type);
  77.  
  78.   bzero (VALUE_CONTENTS (val), TYPE_LENGTH (type));
  79.   VALUE_LVAL (val) = lv;
  80.  
  81.   return val;
  82. }
  83.  
  84. /* Return the value with a specified type located at specified address.  */
  85.  
  86. value
  87. value_at (type, addr)
  88.      struct type *type;
  89.      CORE_ADDR addr;
  90. {
  91.   register value val = allocate_value (type);
  92.   int temp;
  93.  
  94.   temp = read_memory (addr, VALUE_CONTENTS (val), TYPE_LENGTH (type));
  95.   if (temp)
  96.     {
  97.       if (have_inferior_p ())
  98.     print_sys_errmsg ("ptrace", temp);
  99.       /* Actually, address between addr and addr + len was out of bounds. */
  100.       error ("Cannot read memory: address 0x%x out of bounds.", addr);
  101.     }
  102.  
  103.   VALUE_LVAL (val) = lval_memory;
  104.   VALUE_ADDRESS (val) = addr;
  105.  
  106.   return val;
  107. }
  108.  
  109. /* Store the contents of FROMVAL into the location of TOVAL.
  110.    Return a new value with the location of TOVAL and contents of FROMVAL.  */
  111.  
  112. value
  113. value_assign (toval, fromval)
  114.      register value toval, fromval;
  115. {
  116.   register struct type *type = VALUE_TYPE (toval);
  117.   register value val;
  118.   char raw_buffer[MAX_REGISTER_RAW_SIZE];
  119.   char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  120.   int use_buffer = 0;
  121.  
  122.   extern CORE_ADDR find_saved_register ();
  123.  
  124.   COERCE_ARRAY (fromval);
  125.  
  126.   if (VALUE_LVAL (toval) != lval_internalvar)
  127.     fromval = value_cast (type, fromval);
  128.  
  129.   /* If TOVAL is a special machine register requiring conversion
  130.      of program values to a special raw format,
  131.      convert FROMVAL's contents now, with result in `raw_buffer',
  132.      and set USE_BUFFER to the number of bytes to write.  */
  133.  
  134.   if (VALUE_REGNO (toval) >= 0
  135.       && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
  136.     {
  137.       int regno = VALUE_REGNO (toval);
  138.       if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno))
  139.     fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval);
  140.       bcopy (VALUE_CONTENTS (fromval), virtual_buffer,
  141.          REGISTER_VIRTUAL_SIZE (regno));
  142.       REGISTER_CONVERT_TO_RAW (regno, virtual_buffer, raw_buffer);
  143.       use_buffer = REGISTER_RAW_SIZE (regno);
  144.     }
  145.  
  146.   switch (VALUE_LVAL (toval))
  147.     {
  148.     case lval_internalvar:
  149.       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
  150.       break;
  151.  
  152.     case lval_internalvar_component:
  153.       set_internalvar_component (VALUE_INTERNALVAR (toval),
  154.                  VALUE_OFFSET (toval),
  155.                  VALUE_BITPOS (toval),
  156.                  VALUE_BITSIZE (toval),
  157.                  fromval);
  158.       break;
  159.  
  160.     case lval_memory:
  161.       if (VALUE_BITSIZE (toval))
  162.     {
  163.       int val;
  164.       read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  165.                &val, sizeof val);
  166.       modify_field (&val, (int) value_as_long (fromval),
  167.             VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
  168.       write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  169.             &val, sizeof val);
  170.     }
  171.       else if (use_buffer)
  172.     write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  173.               raw_buffer, use_buffer);
  174.       else
  175.     write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  176.               VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
  177.       break;
  178.  
  179.     case lval_register:
  180.       if (VALUE_BITSIZE (toval))
  181.     {
  182.       int val;
  183.  
  184.       read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  185.                    &val, sizeof val);
  186.       modify_field (&val, (int) value_as_long (fromval),
  187.             VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
  188.       write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  189.                 &val, sizeof val);
  190.     }
  191.       else if (use_buffer)
  192.     write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  193.                   raw_buffer, use_buffer);
  194.       else
  195.     write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  196.                   VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
  197.       break;
  198.  
  199.     case lval_reg_frame_relative:
  200.       {
  201.     /* value is stored in a series of registers in the frame
  202.        specified by the structure.  Copy that value out, modify
  203.        it, and copy it back in.  */
  204.     int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
  205.     int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
  206.     int byte_offset = VALUE_OFFSET (toval) % reg_size;
  207.     int reg_offset = VALUE_OFFSET (toval) / reg_size;
  208.     int amount_copied;
  209.     char *buffer = (char *) alloca (amount_to_copy);
  210.     int regno;
  211.     FRAME frame;
  212.     CORE_ADDR addr;
  213.  
  214.     /* Figure out which frame this is in currently.  */
  215.     for (frame = get_current_frame ();
  216.          frame && FRAME_FP (frame) != VALUE_FRAME (toval);
  217.          frame = get_prev_frame (frame))
  218.       ;
  219.  
  220.     if (!frame)
  221.       error ("Value being assigned to is no longer active.");
  222.  
  223.     amount_to_copy += (reg_size - amount_to_copy % reg_size);
  224.  
  225.     /* Copy it out.  */
  226.     for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
  227.           amount_copied = 0);
  228.          amount_copied < amount_to_copy;
  229.          amount_copied += reg_size, regno++)
  230.       {
  231.         addr = find_saved_register (frame, regno);
  232.         if (addr == 0)
  233.           read_register_bytes (REGISTER_BYTE (regno),
  234.                    buffer + amount_copied,
  235.                    reg_size);
  236.         else
  237.           read_memory (addr, buffer + amount_copied, reg_size);
  238.       }
  239.  
  240.     /* Modify what needs to be modified.  */
  241.     if (VALUE_BITSIZE (toval))
  242.       modify_field (buffer + byte_offset,
  243.             (int) value_as_long (fromval),
  244.             VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
  245.     else if (use_buffer)
  246.       bcopy (raw_buffer, buffer + byte_offset, use_buffer);
  247.     else
  248.       bcopy (VALUE_CONTENTS (fromval), buffer + byte_offset,
  249.          TYPE_LENGTH (type));
  250.  
  251.     /* Copy it back.  */
  252.     for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
  253.           amount_copied = 0);
  254.          amount_copied < amount_to_copy;
  255.          amount_copied += reg_size, regno++)
  256.       {
  257.         addr = find_saved_register (frame, regno);
  258.         if (addr == 0)
  259.           write_register_bytes (REGISTER_BYTE (regno),
  260.                     buffer + amount_copied,
  261.                     reg_size);
  262.         else
  263.           write_memory (addr, buffer + amount_copied, reg_size);
  264.       }
  265.       }
  266.       break;
  267.     
  268.  
  269.     default:
  270.       error ("Left side of = operation is not an lvalue.");
  271.     }
  272.  
  273.   /* Return a value just like TOVAL except with the contents of FROMVAL
  274.      (except in the case of the type if TOVAL is an internalvar).  */
  275.  
  276.   if (VALUE_LVAL (toval) == lval_internalvar
  277.       || VALUE_LVAL (toval) == lval_internalvar_component)
  278.     {
  279.       type = VALUE_TYPE (fromval);
  280.     }
  281.  
  282.   val = allocate_value (type);
  283.   bcopy (toval, val, VALUE_CONTENTS (val) - (char *) val);
  284.   bcopy (VALUE_CONTENTS (fromval), VALUE_CONTENTS (val), TYPE_LENGTH (type));
  285.   VALUE_TYPE (val) = type;
  286.   
  287.   return val;
  288. }
  289.  
  290. /* Extend a value VAL to COUNT repetitions of its type.  */
  291.  
  292. value
  293. value_repeat (arg1, count)
  294.      value arg1;
  295.      int count;
  296. {
  297.   register value val;
  298.  
  299.   if (VALUE_LVAL (arg1) != lval_memory)
  300.     error ("Only values in memory can be extended with '@'.");
  301.   if (count < 1)
  302.     error ("Invalid number %d of repetitions.", count);
  303.  
  304.   val = allocate_repeat_value (VALUE_TYPE (arg1), count);
  305.  
  306.   read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
  307.            VALUE_CONTENTS (val),
  308.            TYPE_LENGTH (VALUE_TYPE (val)) * count);
  309.   VALUE_LVAL (val) = lval_memory;
  310.   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
  311.  
  312.   return val;
  313. }
  314.  
  315. value
  316. value_of_variable (var)
  317.      struct symbol *var;
  318. {
  319.   return read_var_value (var, (FRAME) 0);
  320. }
  321.  
  322. /* Given a value which is an array, return a value which is
  323.    a pointer to its first element.  */
  324.  
  325. value
  326. value_coerce_array (arg1)
  327.      value arg1;
  328. {
  329.   register struct type *type;
  330.   register value val;
  331.  
  332.   if (VALUE_LVAL (arg1) != lval_memory)
  333.     error ("Attempt to take address of value not located in memory.");
  334.  
  335.   /* Get type of elements.  */
  336.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
  337.     type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
  338.   else
  339.     /* A phony array made by value_repeat.
  340.        Its type is the type of the elements, not an array type.  */
  341.     type = VALUE_TYPE (arg1);
  342.  
  343.   /* Get the type of the result.  */
  344.   type = lookup_pointer_type (type);
  345.   val = value_from_long (builtin_type_long,
  346.                (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
  347.   VALUE_TYPE (val) = type;
  348.   return val;
  349. }
  350.  
  351. /* Return a pointer value for the object for which ARG1 is the contents.  */
  352.  
  353. value
  354. value_addr (arg1)
  355.      value arg1;
  356. {
  357.   register struct type *type;
  358.   register value val, arg1_coerced;
  359.  
  360.   /* Taking the address of an array is really a no-op
  361.      once the array is coerced to a pointer to its first element.  */
  362.   arg1_coerced = arg1;
  363.   COERCE_ARRAY (arg1_coerced);
  364.   if (arg1 != arg1_coerced)
  365.     return arg1_coerced;
  366.  
  367.   if (VALUE_LVAL (arg1) != lval_memory)
  368.     error ("Attempt to take address of value not located in memory.");
  369.  
  370.   /* Get the type of the result.  */
  371.   type = lookup_pointer_type (VALUE_TYPE (arg1));
  372.   val = value_from_long (builtin_type_long,
  373.         (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
  374.   VALUE_TYPE (val) = type;
  375.   return val;
  376. }
  377.  
  378. /* Given a value of a pointer type, apply the C unary * operator to it.  */
  379.  
  380. value
  381. value_ind (arg1)
  382.      value arg1;
  383. {
  384.   /* Must do this before COERCE_ARRAY, otherwise an infinite loop
  385.      will result */
  386.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF)
  387.     return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
  388.              (CORE_ADDR) value_as_long (arg1));
  389.  
  390.   COERCE_ARRAY (arg1);
  391.  
  392.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
  393.     error ("not implemented: member types in value_ind");
  394.  
  395.   /* Allow * on an integer so we can cast it to whatever we want.  */
  396.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
  397.     return value_at (builtin_type_long,
  398.              (CORE_ADDR) value_as_long (arg1));
  399.   else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
  400.     return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
  401.              (CORE_ADDR) value_as_long (arg1));
  402.   else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF)
  403.     return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
  404.              (CORE_ADDR) value_as_long (arg1));
  405.   error ("Attempt to take contents of a non-pointer value.");
  406. }
  407.  
  408. /* Pushing small parts of stack frames.  */
  409.  
  410. /* Push one word (the size of object that a register holds).  */
  411.  
  412. CORE_ADDR
  413. push_word (sp, buffer)
  414.      CORE_ADDR sp;
  415.      REGISTER_TYPE buffer;
  416. {
  417.   register int len = sizeof (REGISTER_TYPE);
  418.  
  419. #if 1 INNER_THAN 2
  420.   sp -= len;
  421.   write_memory (sp, &buffer, len);
  422. #else /* stack grows upward */
  423.   write_memory (sp, &buffer, len);
  424.   sp += len;
  425. #endif /* stack grows upward */
  426.  
  427.   return sp;
  428. }
  429.  
  430. /* Push LEN bytes with data at BUFFER.  */
  431.  
  432. CORE_ADDR
  433. push_bytes (sp, buffer, len)
  434.      CORE_ADDR sp;
  435.      char *buffer;
  436.      int len;
  437. {
  438. #if 1 INNER_THAN 2
  439.   sp -= len;
  440.   write_memory (sp, buffer, len);
  441. #else /* stack grows upward */
  442.   write_memory (sp, buffer, len);
  443.   sp += len;
  444. #endif /* stack grows upward */
  445.  
  446.   return sp;
  447. }
  448.  
  449. /* Push onto the stack the specified value VALUE.  */
  450.  
  451. CORE_ADDR
  452. value_push (sp, arg)
  453.      register CORE_ADDR sp;
  454.      value arg;
  455. {
  456.   register int len = TYPE_LENGTH (VALUE_TYPE (arg));
  457.  
  458. #if 1 INNER_THAN 2
  459.   sp -= len;
  460.   write_memory (sp, VALUE_CONTENTS (arg), len);
  461. #else /* stack grows upward */
  462.   write_memory (sp, VALUE_CONTENTS (arg), len);
  463.   sp += len;
  464. #endif /* stack grows upward */
  465.  
  466.   return sp;
  467. }
  468.  
  469. /* Perform the standard coercions that are specified
  470.    for arguments to be passed to C functions.  */
  471.  
  472. value
  473. value_arg_coerce (arg)
  474.      value arg;
  475. {
  476.   register struct type *type;
  477.  
  478.   COERCE_ENUM (arg);
  479.  
  480.   type = VALUE_TYPE (arg);
  481.  
  482.   if (TYPE_CODE (type) == TYPE_CODE_INT
  483.       && TYPE_LENGTH (type) < sizeof (int))
  484.     return value_cast (builtin_type_int, arg);
  485.  
  486.   if (type == builtin_type_float)
  487.     return value_cast (builtin_type_double, arg);
  488.  
  489.   return arg;
  490. }
  491.  
  492. /* Push the value ARG, first coercing it as an argument
  493.    to a C function.  */
  494.  
  495. CORE_ADDR
  496. value_arg_push (sp, arg)
  497.      register CORE_ADDR sp;
  498.      value arg;
  499. {
  500.   return value_push (sp, value_arg_coerce (arg));
  501. }
  502.  
  503. /* Perform a function call in the inferior.
  504.    ARGS is a vector of values of arguments (NARGS of them).
  505.    FUNCTION is a value, the function to be called.
  506.    Returns a value representing what the function returned.
  507.    May fail to return, if a breakpoint or signal is hit
  508.    during the execution of the function.  */
  509.  
  510. value
  511. call_function (function, nargs, args)
  512.      value function;
  513.      int nargs;
  514.      value *args;
  515. {
  516.   register CORE_ADDR sp;
  517.   register int i;
  518.   CORE_ADDR start_sp;
  519.   static REGISTER_TYPE dummy[] = CALL_DUMMY;
  520.   REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
  521.   CORE_ADDR old_sp;
  522.   struct type *value_type;
  523.   unsigned char struct_return;
  524.   CORE_ADDR struct_addr;
  525.   struct inferior_status inf_status;
  526.   struct cleanup *old_chain;
  527.  
  528.   if (!have_inferior_p ())
  529.     error ("Cannot invoke functions if the inferior is not running.");
  530.  
  531. #ifndef KGDB
  532.   save_inferior_status (&inf_status, 1);
  533.   old_chain = make_cleanup (restore_inferior_status, &inf_status);
  534.  
  535.   PUSH_DUMMY_FRAME;
  536.  
  537.   old_sp = sp = read_register (SP_REGNUM);
  538.  
  539. #if 1 INNER_THAN 2        /* Stack grows down */
  540.   sp -= sizeof dummy;
  541.   start_sp = sp;
  542. #else                /* Stack grows up */
  543.   start_sp = sp;
  544.   sp += sizeof dummy;
  545. #endif
  546. #endif
  547.  
  548.   {
  549.     register CORE_ADDR funaddr;
  550.     register struct type *ftype = VALUE_TYPE (function);
  551.     register enum type_code code = TYPE_CODE (ftype);
  552.  
  553.     /* If it's a member function, just look at the function
  554.        part of it.  */
  555.  
  556.     /* Determine address to call.  */
  557.     if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
  558.       {
  559.     funaddr = VALUE_ADDRESS (function);
  560.     value_type = TYPE_TARGET_TYPE (ftype);
  561.       }
  562.     else if (code == TYPE_CODE_PTR)
  563.       {
  564.     funaddr = value_as_long (function);
  565.     if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
  566.         || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
  567.       value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
  568.     else
  569.       value_type = builtin_type_int;
  570.       }
  571.     else if (code == TYPE_CODE_INT)
  572.       {
  573.     /* Handle the case of functions lacking debugging info.
  574.        Their values are characters since their addresses are char */
  575.     if (TYPE_LENGTH (ftype) == 1)
  576.       funaddr = value_as_long (value_addr (function));
  577.     else
  578.       /* Handle integer used as address of a function.  */
  579.       funaddr = value_as_long (function);
  580.  
  581.     value_type = builtin_type_int;
  582.       }
  583.     else
  584.       error ("Invalid data type for function to be called.");
  585. #ifdef KGDB
  586.     /* Create a call sequence customized for this function
  587.        and the number of arguments for it.  */
  588.       if (remote_debugging) {
  589.     int    val;
  590.     char argBuffer[512];
  591.     char *ap;
  592.     ap = argBuffer;
  593.     for (i = 0; i < nargs; i++) {
  594.                value arg = value_arg_coerce (args[i]);
  595.                int len = TYPE_LENGTH (VALUE_TYPE (arg));
  596.            if (ap + len > &(argBuffer[512]) )
  597.             break;
  598.            bcopy(VALUE_CONTENTS (arg),ap,len);
  599.            ap += len;
  600.     }
  601.     val = call_remote_function(funaddr,nargs,ap - argBuffer,argBuffer);
  602.     return value_from_long (builtin_type_int, val);
  603.       }
  604. #endif    
  605.  
  606.     /* Are we returning a value using a structure return or a normal
  607.        value return? */
  608.  
  609.     struct_return = using_struct_return (function, funaddr, value_type);
  610.  
  611.     /* Create a call sequence customized for this function
  612.        and the number of arguments for it.  */
  613.     bcopy (dummy, dummy1, sizeof dummy);
  614.     FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, value_type);
  615.   }
  616.  
  617. #ifndef CANNOT_EXECUTE_STACK
  618.   write_memory (start_sp, dummy1, sizeof dummy);
  619.  
  620. #else
  621.   /* Convex Unix prohibits executing in the stack segment. */
  622.   /* Hope there is empty room at the top of the text segment. */
  623.   {
  624.     extern CORE_ADDR text_end;
  625.     static checked = 0;
  626.     if (!checked)
  627.       for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
  628.     if (read_memory_integer (start_sp, 1) != 0)
  629.       error ("text segment full -- no place to put call");
  630.     checked = 1;
  631.     sp = old_sp;
  632.     start_sp = text_end - sizeof dummy;
  633.     write_memory (start_sp, dummy1, sizeof dummy);
  634.   }
  635. #endif /* CANNOT_EXECUTE_STACK */
  636. #ifdef STACK_ALIGN
  637.   /* If stack grows down, we must leave a hole at the top. */
  638.   {
  639.     int len = 0;
  640.  
  641.     /* Reserve space for the return structure to be written on the
  642.        stack, if necessary */
  643.  
  644.     if (struct_return)
  645.       len += TYPE_LENGTH (value_type);
  646.     
  647.     for (i = nargs - 1; i >= 0; i--)
  648.       len += TYPE_LENGTH (VALUE_TYPE (args[i]));
  649. #ifdef CALL_DUMMY_STACK_ADJUST
  650.     len += CALL_DUMMY_STACK_ADJUST;
  651. #endif
  652. #if 1 INNER_THAN 2
  653.     sp -= STACK_ALIGN (len) - len;
  654. #else
  655.     sp += STACK_ALIGN (len) - len;
  656. #endif
  657.   }
  658. #endif /* STACK_ALIGN */
  659.  
  660.     /* Reserve space for the return structure to be written on the
  661.        stack, if necessary */
  662.  
  663.     if (struct_return)
  664.       {
  665. #if 1 INNER_THAN 2
  666.     sp -= TYPE_LENGTH (value_type);
  667.     struct_addr = sp;
  668. #else
  669.     struct_addr = sp;
  670.     sp += TYPE_LENGTH (value_type);
  671. #endif
  672.       }
  673.     
  674.   for (i = nargs - 1; i >= 0; i--)
  675.     sp = value_arg_push (sp, args[i]);
  676.  
  677. #ifdef CALL_DUMMY_STACK_ADJUST
  678. #if 1 INNER_THAN 2
  679.   sp -= CALL_DUMMY_STACK_ADJUST;
  680. #else
  681.   sp += CALL_DUMMY_STACK_ADJUST;
  682. #endif
  683. #endif /* CALL_DUMMY_STACK_ADJUST */
  684.  
  685.   /* Store the address at which the structure is supposed to be
  686.      written.  Note that this (and the code which reserved the space
  687.      above) assumes that gcc was used to compile this function.  Since
  688.      it doesn't cost us anything but space and if the function is pcc
  689.      it will ignore this value, we will make that assumption.
  690.  
  691.      Also note that on some machines (like the sparc) pcc uses this
  692.      convention in a slightly twisted way also.  */
  693.  
  694.   if (struct_return)
  695.     STORE_STRUCT_RETURN (struct_addr, sp);
  696.  
  697.   /* Write the stack pointer.  This is here because the statement above
  698.      might fool with it */
  699.   write_register (SP_REGNUM, sp);
  700.  
  701.   /* Figure out the value returned by the function.  */
  702.   {
  703.     char retbuf[REGISTER_BYTES];
  704.  
  705.     /* Execute the stack dummy routine, calling FUNCTION.
  706.        When it is done, discard the empty frame
  707.        after storing the contents of all regs into retbuf.  */
  708.     run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
  709.  
  710.     do_cleanups (old_chain);
  711.  
  712.     return value_being_returned (value_type, retbuf, struct_return);
  713.   }
  714. }
  715.  
  716. /* Create a value for a string constant:
  717.    Call the function malloc in the inferior to get space for it,
  718.    then copy the data into that space
  719.    and then return the address with type char *.
  720.    PTR points to the string constant data; LEN is number of characters.  */
  721.  
  722. value
  723. value_string (ptr, len)
  724.      char *ptr;
  725.      int len;
  726. {
  727.   register value val;
  728.   register struct symbol *sym;
  729.   value blocklen;
  730.   register char *copy = (char *) alloca (len + 1);
  731.   char *i = ptr;
  732.   register char *o = copy, *ibeg = ptr;
  733.   register int c;
  734.  
  735.   /* Copy the string into COPY, processing escapes.
  736.      We could not conveniently process them in expread
  737.      because the string there wants to be a substring of the input.  */
  738.  
  739.   while (i - ibeg < len)
  740.     {
  741.       c = *i++;
  742.       if (c == '\\')
  743.     {
  744.       c = parse_escape (&i);
  745.       if (c == -1)
  746.         continue;
  747.     }
  748.       *o++ = c;
  749.     }
  750.   *o = 0;
  751.  
  752.   /* Get the length of the string after escapes are processed.  */
  753.  
  754.   len = o - copy;
  755.  
  756.   /* Find the address of malloc in the inferior.  */
  757.  
  758.   sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0);
  759.   if (sym != 0)
  760.     {
  761.       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
  762.     error ("\"malloc\" exists in this program but is not a function.");
  763.       val = value_of_variable (sym);
  764.     }
  765.   else
  766.     {
  767.       register int i;
  768.       for (i = 0; i < misc_function_count; i++)
  769.     if (!strcmp (misc_function_vector[i].name, "malloc"))
  770.       break;
  771.       if (i < misc_function_count)
  772.     val = value_from_long (builtin_type_long,
  773.                  (LONGEST) misc_function_vector[i].address);
  774.       else
  775.     error ("String constants require the program to have a function \"malloc\".");
  776.     }
  777.  
  778.   blocklen = value_from_long (builtin_type_int, (LONGEST) (len + 1));
  779.   val = call_function (val, 1, &blocklen);
  780.   if (value_zerop (val))
  781.     error ("No memory available for string constant.");
  782.   write_memory ((CORE_ADDR) value_as_long (val), copy, len + 1);
  783.   VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
  784.   return val;
  785. }
  786.  
  787. /* Given ARG1, a value of type (pointer to a)* structure/union,
  788.    extract the component named NAME from the ultimate target structure/union
  789.    and return it as a value with its appropriate type.
  790.    ERR is used in the error message if ARG1's type is wrong.
  791.  
  792.    C++: ARGS is a list of argument types to aid in the selection of
  793.    an appropriate method. Also, handle derived types.
  794.  
  795.    ERR is an error message to be printed in case the field is not found.  */
  796.  
  797. value
  798. value_struct_elt (arg1, args, name, err)
  799.      register value arg1, *args;
  800.      char *name;
  801.      char *err;
  802. {
  803.   register struct type *t;
  804.   register int i;
  805.   int found = 0;
  806.  
  807.   struct type *baseclass;
  808.  
  809.   COERCE_ARRAY (arg1);
  810.  
  811.   t = VALUE_TYPE (arg1);
  812.  
  813.   /* Follow pointers until we get to a non-pointer.  */
  814.  
  815.   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
  816.     {
  817.       arg1 = value_ind (arg1);
  818.       COERCE_ARRAY (arg1);
  819.       t = VALUE_TYPE (arg1);
  820.     }
  821.  
  822.   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
  823.     error ("not implemented: member type in value_struct_elt");
  824.  
  825.   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  826.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  827.     error ("Attempt to extract a component of a value that is not a %s.", err);
  828.  
  829.   baseclass = t;
  830.  
  831.   if (!args)
  832.     {
  833.       /*  if there are no arguments ...do this...  */
  834.  
  835.       /*  Try as a variable first, because if we succeed, there
  836.       is less work to be done.  */
  837.       while (t)
  838.     {
  839.       for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
  840.         {
  841.           if (!strcmp (TYPE_FIELD_NAME (t, i), name))
  842.         {
  843.           found = 1;
  844.           break;
  845.         }
  846.         }
  847.  
  848.       if (i >= 0)
  849.         return TYPE_FIELD_STATIC (t, i)
  850.           ? value_static_field (t, name, i) : value_field (arg1, i);
  851.  
  852.       if (TYPE_N_BASECLASSES (t) == 0)
  853.         break;
  854.  
  855.       t = TYPE_BASECLASS (t, 1);
  856.       VALUE_TYPE (arg1) = t; /* side effect! */
  857.     }
  858.  
  859.       /* C++: If it was not found as a data field, then try to
  860.          return it as a pointer to a method.  */
  861.       t = baseclass;
  862.       VALUE_TYPE (arg1) = t;    /* side effect! */
  863.  
  864.       if (destructor_name_p (name, t))
  865.     error ("use `info method' command to print out value of destructor");
  866.  
  867.       while (t)
  868.     {
  869.       for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
  870.         {
  871.           if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
  872.         {
  873.           error ("use `info method' command to print value of method \"%s\"", name);
  874.         }
  875.         }
  876.  
  877.       if (TYPE_N_BASECLASSES (t) == 0)
  878.         break;
  879.  
  880.       t = TYPE_BASECLASS (t, 1);
  881.     }
  882.  
  883.       if (found == 0)
  884.     error("there is no field named %s", name);
  885.       return 0;
  886.     }
  887.  
  888.   if (destructor_name_p (name, t))
  889.     {
  890.       if (!args[1])
  891.     {
  892.       /* destructors are a special case.  */
  893.       return (value)value_fn_field (arg1, 0,
  894.                     TYPE_FN_FIELDLIST_LENGTH (t, 0));
  895.     }
  896.       else
  897.     {
  898.       error ("destructor should not have any argument");
  899.     }
  900.     }
  901.  
  902.   /*   This following loop is for methods with arguments.  */
  903.   while (t)
  904.     {
  905.       /* Look up as method first, because that is where we
  906.      expect to find it first.  */
  907.       for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
  908.     {
  909.       struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
  910.  
  911.       if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
  912.         {
  913.           int j;
  914.           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
  915.  
  916.           found = 1;
  917.           for (j = TYPE_FN_FIELDLIST_LENGTH (t, i) - 1; j >= 0; --j)
  918.         {
  919.           if (!typecmp (TYPE_FN_FIELD_ARGS (f, j), args))
  920.             {
  921.               if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
  922.             return (value)value_virtual_fn_field (arg1, f, j, t);
  923.               else
  924.             return (value)value_fn_field (arg1, i, j);
  925.             }
  926.         }
  927.         }
  928.     }
  929.  
  930.       if (TYPE_N_BASECLASSES (t) == 0)
  931.     break;
  932.       
  933.       t = TYPE_BASECLASS (t, 1);
  934.       VALUE_TYPE (arg1) = t;    /* side effect! */
  935.     }
  936.  
  937.   if (found)
  938.     {
  939.       error ("Structure method %s not defined for arglist.", name);
  940.       return 0;
  941.     }
  942.   else
  943.     {
  944.       /* See if user tried to invoke data as function */
  945.       t = baseclass;
  946.       while (t)
  947.     {
  948.       for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
  949.         {
  950.           if (!strcmp (TYPE_FIELD_NAME (t, i), name))
  951.         {
  952.           found = 1;
  953.           break;
  954.         }
  955.         }
  956.  
  957.       if (i >= 0)
  958.         return TYPE_FIELD_STATIC (t, i)
  959.           ? value_static_field (t, name, i) : value_field (arg1, i);
  960.  
  961.       if (TYPE_N_BASECLASSES (t) == 0)
  962.         break;
  963.  
  964.       t = TYPE_BASECLASS (t, 1);
  965.       VALUE_TYPE (arg1) = t; /* side effect! */
  966.     }
  967.       error ("Structure has no component named %s.", name);
  968.     }
  969. }
  970.  
  971. /* C++: return 1 is NAME is a legitimate name for the destructor
  972.    of type TYPE.  If TYPE does not have a destructor, or
  973.    if NAME is inappropriate for TYPE, an error is signaled.  */
  974. int
  975. destructor_name_p (name, type)
  976.      char *name;
  977.      struct type *type;
  978. {
  979.   /* destructors are a special case.  */
  980.   char *dname = TYPE_NAME (type);
  981.  
  982.   if (name[0] == '~')
  983.     {
  984.       if (! TYPE_HAS_DESTRUCTOR (type))
  985.     error ("type `%s' does not have destructor defined",
  986.            TYPE_NAME (type));
  987.       /* Skip past the "struct " at the front.  */
  988.       while (*dname++ != ' ') ;
  989.       if (strcmp (dname, name+1))
  990.     error ("destructor specification error");
  991.       else
  992.     return 1;
  993.     }
  994.   return 0;
  995. }
  996.  
  997. /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
  998.    return 1 if the component named NAME from the ultimate
  999.    target structure/union is defined, otherwise, return 0.  */
  1000.  
  1001. int
  1002. check_field (arg1, name)
  1003.      register value arg1;
  1004.      char *name;
  1005. {
  1006.   register struct type *t;
  1007.   register int i;
  1008.   int found = 0;
  1009.  
  1010.   struct type *baseclass;
  1011.  
  1012.   COERCE_ARRAY (arg1);
  1013.  
  1014.   t = VALUE_TYPE (arg1);
  1015.  
  1016.   /* Follow pointers until we get to a non-pointer.  */
  1017.  
  1018.   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
  1019.     {
  1020.       arg1 = value_ind (arg1);
  1021.       COERCE_ARRAY (arg1);
  1022.       t = VALUE_TYPE (arg1);
  1023.     }
  1024.  
  1025.   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
  1026.     error ("not implemented: member type in check_field");
  1027.  
  1028.   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  1029.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  1030.     error ("Internal error: `this' is not an aggregate");
  1031.  
  1032.   baseclass = t;
  1033.  
  1034.   while (t)
  1035.     {
  1036.       for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
  1037.     {
  1038.       if (!strcmp (TYPE_FIELD_NAME (t, i), name))
  1039.         {
  1040.           return 1;
  1041.         }
  1042.     }
  1043.       if (TYPE_N_BASECLASSES (t) == 0)
  1044.     break;
  1045.  
  1046.       t = TYPE_BASECLASS (t, 1);
  1047.       VALUE_TYPE (arg1) = t;    /* side effect! */
  1048.     }
  1049.  
  1050.   /* C++: If it was not found as a data field, then try to
  1051.      return it as a pointer to a method.  */
  1052.   t = baseclass;
  1053.   VALUE_TYPE (arg1) = t;    /* side effect! */
  1054.  
  1055.   /* Destructors are a special case.  */
  1056.   if (destructor_name_p (name, t))
  1057.     return 1;
  1058.  
  1059.   while (t)
  1060.     {
  1061.       for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
  1062.     {
  1063.       if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
  1064.         return 1;
  1065.     }
  1066.  
  1067.       if (TYPE_N_BASECLASSES (t) == 0)
  1068.     break;
  1069.  
  1070.       t = TYPE_BASECLASS (t, 1);
  1071.     }
  1072.   return 0;
  1073. }
  1074.  
  1075. /* C++: Given an aggregate type DOMAIN, and a member name NAME,
  1076.    return the address of this member as a pointer to member
  1077.    type.  If INTYPE is non-null, then it will be the type
  1078.    of the member we are looking for.  This will help us resolve
  1079.    pointers to member functions.  */
  1080.  
  1081. value
  1082. value_struct_elt_for_address (domain, intype, name)
  1083.      struct type *domain, *intype;
  1084.      char *name;
  1085. {
  1086.   register struct type *t = domain;
  1087.   register int i;
  1088.   int found = 0;
  1089.   value v;
  1090.  
  1091.   struct type *baseclass;
  1092.  
  1093.   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  1094.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  1095.     error ("Internal error: non-aggregate type to value_struct_elt_for_address");
  1096.  
  1097.   baseclass = t;
  1098.  
  1099.   while (t)
  1100.     {
  1101.       for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
  1102.     {
  1103.       if (!strcmp (TYPE_FIELD_NAME (t, i), name))
  1104.         {
  1105.           if (TYPE_FIELD_PACKED (t, i))
  1106.         error ("pointers to bitfield members not allowed");
  1107.  
  1108.           v = value_from_long (builtin_type_int,
  1109.                    (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
  1110.           VALUE_TYPE (v) = lookup_pointer_type (
  1111.               lookup_member_type (TYPE_FIELD_TYPE (t, i), baseclass));
  1112.           return v;
  1113.         }
  1114.     }
  1115.  
  1116.       if (TYPE_N_BASECLASSES (t) == 0)
  1117.     break;
  1118.  
  1119.       t = TYPE_BASECLASS (t, 1);
  1120.     }
  1121.  
  1122.   /* C++: If it was not found as a data field, then try to
  1123.      return it as a pointer to a method.  */
  1124.   t = baseclass;
  1125.  
  1126.   /* Destructors are a special case.  */
  1127.   if (destructor_name_p (name, t))
  1128.     {
  1129.       error ("pointers to destructors not implemented yet");
  1130.     }
  1131.  
  1132.   /* Perform all necessary dereferencing.  */
  1133.   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
  1134.     intype = TYPE_TARGET_TYPE (intype);
  1135.  
  1136.   while (t)
  1137.     {
  1138.       for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
  1139.     {
  1140.       if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
  1141.         {
  1142.           int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
  1143.           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
  1144.  
  1145.           if (intype == 0 && j > 1)
  1146.         error ("non-unique member `%s' requires type instantiation", name);
  1147.           if (intype)
  1148.         {
  1149.           while (j--)
  1150.             if (TYPE_FN_FIELD_TYPE (f, j) == intype)
  1151.               break;
  1152.           if (j < 0)
  1153.             error ("no member function matches that type instantiation");
  1154.         }
  1155.           else
  1156.         j = 0;
  1157.  
  1158.           if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
  1159.         {
  1160.           v = value_from_long (builtin_type_long,
  1161.                        (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
  1162.         }
  1163.           else
  1164.         {
  1165.           struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
  1166.                             0, VAR_NAMESPACE, 0);
  1167.           v = locate_var_value (s, 0);
  1168.         }
  1169.           VALUE_TYPE (v) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), baseclass));
  1170.           return v;
  1171.         }
  1172.     }
  1173.  
  1174.       if (TYPE_N_BASECLASSES (t) == 0)
  1175.     break;
  1176.  
  1177.       t = TYPE_BASECLASS (t, 1);
  1178.     }
  1179.   return 0;
  1180. }
  1181.  
  1182. /* Compare two argument lists and return the position in which they differ,
  1183.    or zero if equal. Note that we ignore the first argument, which is
  1184.    the type of the instance variable. This is because we want to handle
  1185.    derived classes. This is not entirely correct: we should actually
  1186.    check to make sure that a requested operation is type secure,
  1187.    shouldn't we? */
  1188. int typecmp(t1, t2)
  1189.      struct type *t1[];
  1190.      value t2[];
  1191. {
  1192.   int i;
  1193.  
  1194.   if (t1[0]->code == TYPE_CODE_VOID) return 0;
  1195.   if (!t1[1]) return 0;
  1196.   for (i = 1; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++)
  1197.     {
  1198.       if (! t2[i]
  1199.       || t1[i]->code != t2[i]->type->code
  1200.       || t1[i]->target_type != t2[i]->type->target_type)
  1201.     {
  1202.       return i+1;
  1203.     }
  1204.     }
  1205.   if (!t1[i]) return 0;
  1206.   return t2[i] ? i+1 : 0;
  1207. }
  1208.  
  1209. /* C++: return the value of the class instance variable, if one exists.
  1210.    Flag COMPLAIN signals an error if the request is made in an
  1211.    inappropriate context.  */
  1212. value
  1213. value_of_this (complain)
  1214.      int complain;
  1215. {
  1216.   extern FRAME selected_frame;
  1217.   struct symbol *func, *sym;
  1218.   char *funname = 0;
  1219.   struct block *b;
  1220.   int i;
  1221.  
  1222.   if (selected_frame == 0)
  1223.     if (complain)
  1224.       error ("no frame selected");
  1225.     else return 0;
  1226.  
  1227.   func = get_frame_function (selected_frame);
  1228.   if (func)
  1229.     funname = SYMBOL_NAME (func);
  1230.   else
  1231.     if (complain)
  1232.       error ("no `this' in nameless context");
  1233.     else return 0;
  1234.  
  1235.   b = SYMBOL_BLOCK_VALUE (func);
  1236.   i = BLOCK_NSYMS (b);
  1237.   if (i <= 0)
  1238.     if (complain)
  1239.       error ("no args, no `this'");
  1240.     else return 0;
  1241.  
  1242.   sym = BLOCK_SYM (b, 0);
  1243.   if (strncmp ("$this", SYMBOL_NAME (sym), 5))
  1244.     if (complain)
  1245.       error ("current stack frame not in method");
  1246.     else return 0;
  1247.  
  1248.   return read_var_value (sym, selected_frame);
  1249. }
  1250.